package com.dly.blog.service.impl;

import com.dly.blog.domain.Topic;
import com.dly.blog.mapper.TopicMapper;
import com.dly.blog.service.TopicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.dly.blog.domain.vo.topic.TopicVO;
import com.dly.blog.domain.ro.topic.TopicPageRO;
import com.dly.blog.domain.ro.topic.TopicAddRO;
import com.dly.blog.domain.ro.topic.TopicEditRO;
import com.dly.blog.domain.vo.PageVO;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import lombok.AllArgsConstructor;
import cn.hutool.core.collection.ListUtil;

/**
 * <p>
 * 主题 服务实现类
 * </p>
 *
 * @author DLY
 * @since 2024-01-24 09:53:18
 */
@Service
@AllArgsConstructor
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {

    @Override
    public PageVO<TopicVO> getTopicPage(TopicPageRO topicPageRO) {
        Page<Topic> page = baseMapper.selectPage(topicPageRO.buildIPage(), new LambdaQueryWrapper<>());
        return PageVO.buildMyBatis(page, topic -> {
            TopicVO topicVO = new TopicVO();
            BeanUtils.copyProperties(topic, topicVO);
            return topicVO;
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addTopic(TopicAddRO topicAddRO) {
        Topic topic = new Topic();
        BeanUtils.copyProperties(topicAddRO, topic);
        baseMapper.insert(topic);
    }

    @Override
    public TopicVO getTopic(String id) {
        Topic topic = baseMapper.selectById(id);
        TopicVO topicVO = new TopicVO();
        BeanUtils.copyProperties(topic, topicVO);
        return topicVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeTopic(String[] ids) {
        baseMapper.deleteBatchIds(ListUtil.toList(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editTopic(TopicEditRO topicEditRO) {
        Topic topic = new Topic();
        BeanUtils.copyProperties(topicEditRO, topic);
        baseMapper.updateById(topic);
    }

}
